﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace DevExpress.MailClient.Win.Modules
{
    public partial class RegionMap : BaseModule
    {
        public RegionMap()
        {
            InitializeComponent();
        }

        private void RegionMap_Load(object sender, EventArgs e)
        {
            MAP_CATEGORIES = new Dictionary<int, PRO_DOCS.DAL.Database.MapCategory>();
            //1. get all categories pins and locations data
            var flusher = new LoadLocations(GET_ALL_REGIONS_WITH_CATEGORIES);
            //2. 
            flusher.BeginInvoke(ShowLocations, flusher);
        }

        private delegate List<PRO_DOCS.DAL.Database.SelectAllRegionsWithCatResult> LoadLocations();

        private void ShowLocations(IAsyncResult result)
        {
            var flusher = (LoadLocations)result.AsyncState;
            var res = flusher.EndInvoke(result);
            gridControl1.BeginInvoke(new Action(delegate
                                                    {
                                                        gridControl1.DataSource = res;
                                                    }));
            this.BeginInvoke(
                new Action(
                    () =>
                        ucGeoMap1.InitGeoMap("Ataoi47xOQRPCvJHx5027VI_-6G5iweaS_5P11uYz2vOVEk1pdhR9wm3ghYQyDp2",
                            res.Select(singleLocation => new DevExpress.MailClient.Win.Controls.eMap.GeoLocation
                            {
                                CategoryId = singleLocation.MapCategoryID.Value,
                                CatName = singleLocation.MapCategoryName,
                                ImagePath = GET_PIN_IMAGE_PATH(singleLocation.MapRegionImage, singleLocation.MapCategoryID.Value, false),
                                Latitude = singleLocation.MapRegionLatitide.Value,
                                Longitude = singleLocation.MapRegionLogitude.Value,
                                RegionName = singleLocation.MapRegionName,
                                IconPath = GET_PIN_IMAGE_PATH(singleLocation.MapCategoryIcon, singleLocation.MapCategoryID.Value, true),
                                ImgInd = 0
                            }).ToList())));

        }

        Dictionary<int, PRO_DOCS.DAL.Database.MapCategory> MAP_CATEGORIES = new Dictionary<int, PRO_DOCS.DAL.Database.MapCategory>();

        private string GET_PIN_IMAGE_PATH(string _icon_image, int map_category_id, bool isCategoryIcon)
        {
            string image_file_path = (isCategoryIcon) ? @"Resources\country_32x32.png" : @"Resources\no-image.png";


            if (!MAP_CATEGORIES.ContainsKey(map_category_id))
            {
                PRO_DOCS.DAL.Database.MapCategory mapCategory = (from category in PRO_DOCSDatabaseDataContext.MapCategories
                                                                 where category.MapCategoryID.Equals(map_category_id)
                                                                 select category).FirstOrDefault();

                MAP_CATEGORIES.Add(map_category_id, mapCategory);

            }

            int _aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID = MAP_CATEGORIES[map_category_id].ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Value;

            List<string> file_pathes = (from pathes in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs
                                        where pathes.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Value.Equals(_aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID)
                                        select pathes.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH).ToList();

            for (int i = 0; i < file_pathes.Count; i++)
            {
                FileInfo fiImage = (isCategoryIcon) ? new FileInfo(Path.Combine(file_pathes[i], _icon_image)) : new FileInfo(Path.Combine(file_pathes[i], "thumb_" + _icon_image));

                if (fiImage.Exists)
                {
                    image_file_path = fiImage.FullName;
                    break;
                }
            }

            return image_file_path;
        }

        private List<PRO_DOCS.DAL.Database.SelectAllRegionsWithCatResult> GET_ALL_REGIONS_WITH_CATEGORIES()
        {
            return PRO_DOCSDatabaseDataContext.SelectAllRegionsWithCat().ToList();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var region = gridView1.GetFocusedRow() as PRO_DOCS.DAL.Database.SelectAllRegionsWithCatResult;
            if (region != null)
            {
                if (region.MapRegionLatitide != null && region.MapRegionLogitude != null)
                    ucGeoMap1.CenterTo(region.MapRegionLogitude.Value, region.MapRegionLatitide.Value);
            }
        }
    }
}
